﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SKYPE4COMLib;

namespace skypeConnector.Model
{

    class SkypeUtil
    {
        private ISkype skype { get; set; }
        
        private static GroupCollection customGroups;

        public SkypeUtil()
        {
            skype = new Skype();
            refreshCustomGroups(); 
        }

        public bool connectToSkype()
        {
            if (!skype.Client.IsRunning)
            {
                skype.Client.Start(false, false);
            }
            skype.Attach(6, true);
            return skype.AttachmentStatus !=
                skype.Convert.TextToAttachmentStatus("SUCCESS");
            
        }

        public void refreshCustomGroups()
        {
            customGroups = getAllSkypeCustomGroups();
        }

        public GroupCollection getAllSkypeCustomGroups()
        {
            return skype.CustomGroups;
        }

        public GroupCollection getAllCustomGroups()
        {
            return customGroups;
        }

        public string getUserNameToDispaly(User user)
        {
            string dispName = "";
            if (user.Handle.Length != 0)
            {
                dispName += user.Handle;
                dispName += "(";
                dispName += user.FullName;
                dispName += ")";
            }
            return dispName;
        }

        public string getUserSexToDisplay(TUserSex sex)
        {
            switch (sex)
            {
                case TUserSex.usexFemale:
                    return "жен";
                case TUserSex.usexMale:
                    return "муж";
                case TUserSex.usexUnknown:
                default:
                    return "";
            }
        }

        public bool isGroupExists(string groupName)
        {
            foreach (Group gr in skype.CustomGroups)
            {
                if (gr.DisplayName == groupName)
                    return true;
            }
            return false;
        }

        public void addGroup(string groupName)
        {
            if (groupName.Length == 0)
                return;
            Group gr = skype.CreateGroup(groupName);
            skype.Groups.Add(gr);
        }

        public Group getGroupByName(string groupName)
        {
            if (groupName.Length == 0)
                return null;
            foreach (Group gr in skype.CustomGroups)
            {
                if (gr.DisplayName == groupName)
                    return gr;
            }
            return null;
        }

        public void deleteGroup(string groupName)
        {
            if (groupName.Length == 0)
                return;
            Group gr = getGroupByName(groupName);
            if (gr != null)
                skype.DeleteGroup(gr.Id);
        }

        public UserCollection findUsersByFirstName(string firstName)
        {
            UserCollection userCol;
            if (firstName.Length == 0)
                userCol = null;
            else
            {
                userCol = skype.SearchForUsers(firstName);
            }
            return userCol;
        }

        public UserCollection filterUsersBySex(TUserSex sex, UserCollection users)
        {
            UserCollection toReturn = new UserCollection();
            if (sex == TUserSex.usexUnknown)
                return users;
            foreach (User user in users)
            {
  
                if (user.Sex == sex)
                    toReturn.Add(user);
            }
            return toReturn;
        }

        public UserCollection filterUsersByCountry(string country, UserCollection users)
        {
            UserCollection toReturn = new UserCollection();
            string l_country = country.ToLower();
            foreach (User user in users)
            {
                if (user.Country.ToLower() == l_country)
                    toReturn.Add(user);
            }
            return toReturn;
        }

        public UserCollection getFrieds()
        {
            return skype.Friends;
        }

        public bool isUserInGroups(User user)
        {
            foreach (Group group in getAllCustomGroups())
            {
                foreach (User userGroup in group.Users)
                {
                    if (user.Handle == userGroup.Handle)
                        return true;
                }
            }
            return false;
        }

        public bool isUserOnline(User user)
        {
            if (user.OnlineStatus == TOnlineStatus.olsOnline ||
                    user.OnlineStatus == TOnlineStatus.olsAway ||
                    user.OnlineStatus == TOnlineStatus.olsSkypeOut ||
                    user.OnlineStatus == TOnlineStatus.olsSkypeMe ||
                    user.OnlineStatus == TOnlineStatus.olsDoNotDisturb)
            {
                return true;
            }
            return false;
        }

        public User getUserBySkypeId(string id)
        {
            foreach (User user in skype.Friends)
                if (user.Handle == id)
                    return user;
            return null;
        }

        public string getDateString(DateTime date)
        {
            string result = date.Day.ToString();
            result += "." + date.Month.ToString();
            result += "." + date.Year.ToString();
            return result;
        }

        public void addUsersToGroup(string groupName, List<string> col)
        {
            Group group = getGroupByName(groupName);
            foreach (string user in col)
                group.AddUser(user);
            
        }

    }
}
